﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyClassLibrary;

namespace MyProgram
{
    public partial class FrmPersonProfile : Form
    {
        clsUser user = null;

        public FrmPersonProfile(clsUser user)
        {
            InitializeComponent();

            this.user = user;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.splitContainer2.Panel1Collapsed =! this.splitContainer2.Panel1Collapsed;
        }

        private void FrmPersonProfile_Load(object sender, EventArgs e)
        {
            this.splitContainer2.Panel1Collapsed = true;

            this.txb_UserName.Text = this.user.userName;
            this.txb_UserAccount.Text = this.user.userAccount;
            this.txb_UserEmail.Text = this.user.userEmail;

            this.cbb_UserJobID.DataSource = user.jobTitles();
            this.cbb_UserJobID.SelectedIndex = this.user.userJobID - 1;

            this.cbb_UserDivisionID.DataSource = user.divisionType();
            this.cbb_UserDivisionID.SelectedIndex = this.user.userDivisionID - 1;

            this.txb_UserPhoneNumber.Text = this.user.userPhoneNumber.ToString("D10");
        }

        private void btn_Modify_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("確定修改個人資料", "修改確認", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
            {
                this.user.userEmail = this.txb_UserEmail.Text;
                this.user.userJobID = this.cbb_UserJobID.SelectedIndex + 1;
                this.user.userDivisionID = this.cbb_UserDivisionID.SelectedIndex + 1;

                try
                {
                    this.user.userPhoneNumber = Convert.ToInt32(this.txb_UserPhoneNumber.Text);
                }
                catch (Exception)
                {
                    MessageBox.Show("電話號碼格式錯誤，請重新輸入");
                    this.txb_UserPhoneNumber.Focus();
                    this.txb_UserPhoneNumber.SelectAll();
                    return;
                }

                if (this.txb_UserNewPwd.Text != "")
                {
                    if (user.checkPassword(this.txb_UserOldPwd.Text))
                    {
                        if (this.txb_UserNewPwdCheck.Text == this.txb_UserNewPwd.Text)
                        {
                            this.user.userPwd = this.user.CreatePwd(this.txb_UserNewPwd.Text, this.user.userSalt);
                        }
                        else
                        {
                            MessageBox.Show("新密碼前後輸入不一致，請重新輸入", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            this.txb_UserNewPwd.Clear();
                            this.txb_UserNewPwdCheck.Clear();
                            this.txb_UserNewPwd.Focus();
                        }
                    }
                    else
                    {
                        MessageBox.Show("原始密碼輸入錯誤，請重新輸入", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        this.txb_UserOldPwd.Clear();
                        this.txb_UserNewPwd.Clear();
                        this.txb_UserNewPwdCheck.Clear();
                        this.txb_UserOldPwd.Focus();
                    }
                }
                else if (user.updateUserProfile())
                {
                    MessageBox.Show("修改完成");
                    FrmPersonProfile_Load(new object(), new EventArgs());
                }
                else
                {
                    MessageBox.Show("修改失敗，請通知帳號管理員");
                }
            }

            ClsRecord.Record(sender, this.Name);
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("確認取消更新個人資料？", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
            {
                FrmPersonProfile_Load(new object(), new EventArgs());
            }
            else
            {
                return;
            }
        }
    }
}